//
//  LivingViewController.m
//  SheShiDu
//
//  Created by 单剑秋 on 16/6/11.
//  Copyright © 2016年 单剑秋. All rights reserved.
//

#import "LivingViewController.h"
#import "LivingTableViewCell.h"
#import "JQImageView.h"
#import <UIImageView+WebCache.h>

#define HEIGHT self.view.frame.size.height
#define WIDTH self.view.frame.size.width
typedef NS_ENUM(NSUInteger, Diary){
    JingXuan,
    ZuiXin,
};

@interface LivingViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *picArr;
@property (nonatomic, strong)NSMutableArray *newpicArr;
@property (nonatomic, assign)NSInteger i;

@end

@implementation LivingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 打印屏幕尺寸
//    NSLog(@"%@", NSStringFromCGRect(self.view.frame));
    
    // 分段控制器
    [self uiSegControl];
    
    // 右侧按钮
    [self rightBarbuttonItem];
    
    [self createTableView];
}

- (void)uiSegControl
{
    UISegmentedControl *seg =[[UISegmentedControl alloc] initWithItems:@[@"精选", @"最新"]];
    seg.frame = CGRectMake(0, 0, WIDTH * (328 / 750.0), HEIGHT * (70 / 1334.0));
    seg.selectedSegmentIndex = 0;
    self.navigationItem.titleView = seg;
    seg.backgroundColor = [UIColor whiteColor];
    seg.tintColor = [UIColor colorWithRed:28 / 255.0 green:186 / 255.0 blue:173 / 255.0 alpha:1];
    [seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
}

- (void)rightBarbuttonItem
{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"write"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:28 / 255.0 green:185 /255.0 blue:172 /255.0 alpha:1];
}

- (void)rightAction:(UIBarButtonItem *)rightAction
{
    NSLog(@"写你麻痹");
}

- (void)createTableView
{
    self.picArr = [NSMutableArray array];
    for (int i = 0; i < 5; i++) {
        NSString *pic = [NSString stringWithFormat:@"tu%d", i];
        [_picArr addObject:[UIImage imageNamed:pic]];
    }
    
    self.newpicArr = [NSMutableArray array];
    for (int i = 5; 5 <= i && i < 13; i++) {
        NSString *pic = [NSString stringWithFormat:@"tu%d", i];
        [_newpicArr addObject:[UIImage imageNamed:pic]];
    }
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 49 - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    // 注册重用池
    [_tableView registerClass:[LivingTableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_i == 0) {
        return 5;
    } else {
        return 8;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_i == 0) {
        LivingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        if (indexPath.length < 5) {
            [cell.bottomImgView setImage:[_picArr objectAtIndex:indexPath.item]];
        }
        cell.userBgImgView.image = [JQImageView jq_captureCircleImage:[UIImage imageNamed:@"host"]];
        //    cell.userImgView.image = [UIImage imageNamed:@"user"];
        cell.titleLabel.text = @"我的装修之旅";
        cell.scaleLabel.text = @"19.5万/144m^2/三居/中式";
        cell.browseImgView.image = [UIImage imageNamed:@"look"];
        cell.browseLabel.text = @"1001";
        cell.replyImgView.image = [UIImage imageNamed:@"reply"];
        cell.replyLabel.text = @"998";
        return cell;

    } else {
        LivingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        if ( indexPath.length < 8) {
            [cell.bottomImgView setImage:[_newpicArr objectAtIndex:indexPath.item]];
        }        cell.userBgImgView.image = [JQImageView jq_captureCircleImage:[UIImage imageNamed:@"host"]];
        //    cell.userImgView.image = [UIImage imageNamed:@"user"];
        cell.titleLabel.text = @"我的装修之旅";
        cell.scaleLabel.text = @"19.5万/144m^2/三居/中式";
        cell.browseImgView.image = [UIImage imageNamed:@"look"];
        cell.browseLabel.text = @"1001";
        cell.replyImgView.image = [UIImage imageNamed:@"reply"];
        cell.replyLabel.text = @"998";
        return cell;
    } 
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHT * (366 / 1334.0);
}

- (void)segAction:(UISegmentedControl *)diary
{
    switch (diary.selectedSegmentIndex) {
        case JingXuan:
            _i = 0;
            [self.tableView removeFromSuperview];
            [self createTableView];
            break;
        case ZuiXin:
            _i = 1;
            [self.tableView removeFromSuperview];
            [self createTableView];
            break;
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
